package com.bw.gmall.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.common.base.BaseApp;
import com.bw.gmall.realtime.common.bean.StorePopularityWindowBean;
import com.bw.gmall.realtime.common.constant.Constant;
import com.bw.gmall.realtime.common.function.DorisMapFunction;
import com.bw.gmall.realtime.common.util.DateFormatUtil;
import com.bw.gmall.realtime.common.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.RichAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class DwsStorePopularityWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsStorePopularityWindow().start(10026, 4, Constant.DWS_STORE_POPULARITY_WINDOW, Constant.TOPIC_DWD_STORE_POPULARITY);
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        //1.etl
        SingleOutputStreamOperator<JSONObject> etl = getId(stream);
        //2.分组
        SingleOutputStreamOperator<StorePopularityWindowBean> process = extracted(etl);
        //3.设置水位线 开窗、聚合
        SingleOutputStreamOperator<StorePopularityWindowBean> reduce = getReduce(process);
        reduce.print();
        //4）写入 Doris
        reduce.map(new DorisMapFunction<>()).sinkTo(FlinkSinkUtil.getDorisSink(Constant.DWS_STORE_POPULARITY_WINDOW));
    }

    private static SingleOutputStreamOperator<StorePopularityWindowBean> getReduce(SingleOutputStreamOperator<StorePopularityWindowBean> process) {
        return process.assignTimestampsAndWatermarks(WatermarkStrategy
                        .<StorePopularityWindowBean>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                        .withTimestampAssigner(new SerializableTimestampAssigner<StorePopularityWindowBean>() {
                            @Override
                            public long extractTimestamp(StorePopularityWindowBean bean, long l) {
                                return bean.getTs();
                            }
                        }))
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<StorePopularityWindowBean>() {
                    @Override
                    public StorePopularityWindowBean reduce(StorePopularityWindowBean t1, StorePopularityWindowBean t2) throws Exception {
                        t1.setCartAddUserCount(t1.getCartAddUserCount() + t2.getCartAddUserCount());
                        t1.setPaidBuyerCount(t1.getPaidBuyerCount() + t2.getPaidBuyerCount());
                        t1.setPaymentAmount(t1.getPaymentAmount() + t2.getPaymentAmount());
                        return t1;
                    }
                }, new AllWindowFunctionWithHistory());
    }

    private static SingleOutputStreamOperator<StorePopularityWindowBean> extracted(SingleOutputStreamOperator<JSONObject> etl) {
        return etl.keyBy(new KeySelector<JSONObject, String>() {
            @Override
            public String getKey(JSONObject jsonObject) throws Exception {
                return jsonObject.getString("user_id");
            }
        }).process(new KeyedProcessFunction<String, JSONObject, StorePopularityWindowBean>() {
            @Override
            public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, StorePopularityWindowBean>.Context context, Collector<StorePopularityWindowBean> collector) throws Exception {
                long ts = jsonObject.getLong("ts") * 1000;
                long cartAddUserCount = jsonObject.getString("is_cart_add").equals("1")? 1 : 0;
                long paidBuyerCount = jsonObject.getString("is_paid").equals("1")? 1 : 0;
                double paymentAmount = jsonObject.getDoubleValue("payment_amount");
                collector.collect(StorePopularityWindowBean.builder()
                        .cartAddUserCount(cartAddUserCount)
                        .paidBuyerCount(paidBuyerCount)
                        .paymentAmount(paymentAmount)
                        .ts(ts)
                        .build());
            }
        });
    }

    private static SingleOutputStreamOperator<JSONObject> getId(DataStreamSource<String> stream) {
        return stream.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String s, Collector<JSONObject> collector) throws Exception {
                try {
                    JSONObject jsonObject = JSONObject.parseObject(s);
                    String user_id = jsonObject.getString("user_id");
                    if (user_id != null) {
                        collector.collect(jsonObject);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    static class AllWindowFunctionWithHistory extends RichAllWindowFunction<StorePopularityWindowBean, StorePopularityWindowBean, TimeWindow> {
        private transient ValueState<StorePopularityWindowBean> historyState;

        @Override
        public void open(Configuration parameters) throws Exception {
            ValueStateDescriptor<StorePopularityWindowBean> descriptor = new ValueStateDescriptor<>(
                    "historyData",
                    StorePopularityWindowBean.class
            );
            historyState = getRuntimeContext().getState(descriptor);
        }

        @Override
        public void apply(TimeWindow timeWindow, Iterable<StorePopularityWindowBean> iterable, Collector<StorePopularityWindowBean> collector) throws Exception {
            StorePopularityWindowBean currentBean = iterable.iterator().next();
            currentBean.setStt(DateFormatUtil.tsToDateTime(timeWindow.getStart()));
            currentBean.setEdt(DateFormatUtil.tsToDateTime(timeWindow.getEnd()));
            currentBean.setCurDate(DateFormatUtil.tsToDate(System.currentTimeMillis()));

            // 获取历史数据
            StorePopularityWindowBean historyBean = historyState.value();

            if (historyBean != null) {
                // 计算较昨日变化率
                currentBean.setCartAddUserCountChange(calculateChangeRate(currentBean.getCartAddUserCount(), historyBean.getCartAddUserCount()));
                currentBean.setPaidBuyerCountChange(calculateChangeRate(currentBean.getPaidBuyerCount(), historyBean.getPaidBuyerCount()));
                currentBean.setPaymentAmountChange(calculateChangeRate(currentBean.getPaymentAmount(), historyBean.getPaymentAmount()));
            } else {
                // 没有历史数据，较昨日变化率为 0
                currentBean.setCartAddUserCountChange(0.0);
                currentBean.setPaidBuyerCountChange(0.0);
                currentBean.setPaymentAmountChange(0.0);
            }

            // 更新历史数据
            historyState.update(currentBean);

            collector.collect(currentBean);
        }

        private double calculateChangeRate(double currentValue, double historyValue) {
            if (historyValue == 0) {
                return 0.0;
            }
            return (currentValue - historyValue) / historyValue;
        }
    }
}